package com.ai.sml.interceptor;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;

import com.ai.common.xml.bean.Action;
import com.ai.common.xml.bean.Input;
import com.ai.common.xml.util.ControlConstants;
import com.ai.common.xml.util.ControlRequestUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;
import com.ai.frame.util.JsonUtil;
import com.ai.sml.util.DateUtil;
import com.ai.sml.util.MyOwnRuntimeException;
import com.ai.sml.util.StringUtil;
import com.ai.sml.util.WebUtils;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 操作日志拦截器
 * 
 * @author ShenHuaJie
 */
@SuppressWarnings("serial")
public class OperationLogInterceptor implements Interceptor {
	private static final Logger logger = LoggerFactory.getActionLog(OperationLogInterceptor.class);
	private String projectName = "flowdis";

	@Override
	public void destroy() {
	}

	@Override
	public void init() {
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		try { // 保存操作日志
			String requestName = getReuestName();
			if (!requestName.contains("查询")) {
				HttpServletRequest request = ServletActionContext.getRequest();
				String requestUrl = request.getRequestURI();
				Subject subject = SecurityUtils.getSubject();
				String opratorId = (String) subject.getPrincipal();
				if(StringUtil.isEmpty(opratorId))
				{
					opratorId="zyzx";
				}
				String staffName = (String) subject.getSession().getAttribute("linkNm");
				if (StringUtil.isEmpty(staffName)) {
					staffName = subject.getSession().getId().toString();
				}
				String clientIp = WebUtils.getCurrentIP(request);
				String clientAgent = request.getHeader("User-Agent");
				String createTime = DateUtil.date2String(new Date());

				InputObject inputObj = new InputObject();
				inputObj.setService("operationLogService");
				inputObj.setMethod("insertOperationLog");
				Map<String, String> params = inputObj.getParams();
				params.put("projectName", projectName);
				params.put("requestUrl", requestUrl);
				params.put("requestName", requestName);
				params.put("clientIp", clientIp);
				params.put("clientAgent", clientAgent);
				params.put("opratorId", opratorId);
				params.put("opratorName", staffName);
				params.put("createTime", createTime);
				// 转换成Json
				String json = JsonUtil.inputObject2Json(inputObj);
				// 调用WebApp服务
//				String result ="";.. FlowcoreManager.pubMethod(json);
//				logger.info("保存日志结果", result);
			}
		} catch (Exception e) {
			logger.error("saveLog", "保存日志失败", e);
		}
		return invocation.invoke();
	}

	// 获取功能名称
	private String getReuestName() throws MyOwnRuntimeException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String path = request.getRequestURI();
		if (path.startsWith(request.getContextPath())) {
			path = path.substring(request.getContextPath().length(), path.length());
		}

		// 根据请求的路径取得配置的Action
		Action action;
		try {
			action = ControlRequestUtil.getActionByPath(path);
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
		// 获取特定的请求参数UID
		String uid = request.getParameter(ControlConstants.UID);
		if (StringUtil.isEmpty(uid)) {
			uid = null;
		}
		if (action != null) {
			Input input;
			try {
				input = ControlRequestUtil.getInputByUID(uid, action);
			} catch (Exception e) {
				throw new MyOwnRuntimeException(e);
			}
			return input.getDesc();
		}
		return null;
	}
}
